var path ;

$(function(){
	getPath(path) ;
	indexMenuView();
});

//获得项目根路径
function getPath(path) {this.path = path ;}

var firstfun ;
/** 显示菜单列表 */
function indexMenuView() {
	
	var tabs = $(".top_left_butt").children() ;
	
	//默认第一个选中
	var ab = tabs.eq(1).children("div div") ;
	ab.attr("class","selected")
	gotoURL(ab.children("a").attr("hreflang")) ;
	//加载菜单列表
	parseMenuXml(ab.children("a").attr("name")) ;
	
	//为所有的子元素添加click事件
	tabs.click(firstfun = function() {
		$("#second_tabs_7").empty() ;
		var tab = $(this).children("div") ;
		
		tabs.children("div div").removeAttr("class") ;
		tabs.eq(0).children("div div").attr("class","menu_head") ;
		tab.attr("class","selected") ;
		
		//打开系统首页
		gotoURL(tab.children("a").attr("hreflang")) ;
		//加载菜单列表
		parseMenuXml(tab.children("a").attr("name")) ;
	});
	
	//移除第一个元素的click
	tabs.eq(0).unbind("click",firstfun);
	//在绑定新的click
	tabs.eq(0).click(function() {
		var url = $(this).attr("id") ;
		//解析菜单列表
		//parseMenuXml(url) ;
		//打开菜单列表
		indexMenuListView(url);
	});
}

/** 获得页面传过来的数据 */
/*function receiveParam() {
	var person_msg = $("#person_msg") ;
	var url = path + "/loginSystem!getEmployeeInfo.action?d="+new Date().getTime() ;
	$.post(url,null,function(json){
		var data = eval(json) ;
		alert(data.emp.empRealname) ;
		person_msg.attr("title","部ccc门：" + data.emp.dept.deptName) ;
		person_msg.text(data.emp.empRealname) ;
	},"json");
	
	//person_msg.attr("title","部门：" + decodeURIComponent(request("deptName"))) ;
	//person_msg.text(decodeURIComponent(request("empRealname"))) ;
	
}*/

/** 解析XML菜单 */
function parseMenuXml(tree) {
	
	var first_panel = $("#first_panel") ;
	
	$.post(path+"/xml_tree/"+tree+".xml",null,function(data){
		
		var dataXml = $(data) ;
		
		var treeRoot = dataXml.find("tree") ;
		
		//获得关联的功能菜单文件名
		var funcbarName = treeRoot.attr("joint") ;
		
		var treeItem = treeRoot.find("item") ;
	
		var _ul1 = $("<ul>") ; _ul1.addClass("first_menu") ;
		
		treeItem.each(function(i){
			
			var item = $(this) ;
			if("item_root" == item.attr("id")) {
				var _li1 = $("<li>") ;
				_li1.attr("id",item.attr("child")) ;
				var a1 = "<a id='m01' hidefocus='hidefocus' href='javascript:;'><img align='absMiddle' src='"+path+"/images/backui/mytable.png'>"+item.attr("text")+"</a>";
				
				_li1.html(a1) ;
				_ul1.append(_li1) ;
			}
			
		});
		
		var second_panel_menu = $(".second-panel-menu") ;
		_ul1.children("li").click(function() {
			var selectItem = $(this) ;
			var _ul2 = $("<ul>") ; _ul2.attr("id","second_menu") ;
			var _ul3 = $("<ul>") ; 
			
			treeItem.each(function(i){
				var item = $(this) ;
				
				if(selectItem.attr("id") == item.attr("parent")) {
					var _li2 = $("<li>") ;
					_li2.attr("id",item.attr("child")) ;
				
					var a2 = "<a hreflang='"+(undefined == item.attr("child")?item.text():'')+"' class='expand' hidefocus='hidefocus' href='javascript:;'><span>"+item.attr("text")+"</span></a>";

					_li2.html(a2) ;
					_ul2.append(_li2) ;
					
					//三级菜单
					if(undefined != item.attr("child")){ 
						var a3 = "";
						treeItem.each(function(i){
							var item = $(this) ;
							if(_li2.attr("id") == item.attr("parent")) {
								var url = item.text().indexOf("http") == -1?path+"/"+item.text():item.text() ;
								a3 += "<li><a hreflang='"+item.text()+"' class='expand' hidefocus='hidefocus' href='javascript:gotoURL(\""+url+"\");'><span>"+item.attr("text")+"</span></a></li>";
							}
						});
					}
					if(undefined != item.attr("child")){
						_li2.attr("class","cid"+i) ;
						_li2.append("<ul id='cid"+i+"' style='display: none;'>"+a3+"</ul>") ;
						
					}
					//三级菜单显示与隐藏，如果不是三级菜单，当点击时打开页面
					_li2.click(function(){
						var gotouri = $(this).children("a") ;
						var url = gotouri.attr("hreflang") ;
						if("" == url) {
							if($("#"+$(this).attr("class")).is(":hidden")) {
								$("#"+$(this).attr("class")).show() ;
							} else {
								//$("#"+$(this).attr("class")).hide() ;
								$("#start_menu_panel").hide() ;
								$("#close").hide() ;
							}
						} else {
							$("#start_menu_panel").hide() ;$("#close").hide() ;
							gotoURL(url.indexOf("http") == -1?path+"/"+url:url) ;
							parseFuncBar(funcbarName,item.attr("id")) ;
						}
					});
					_ul3.children("li").click(function() {
						var gotouri = $(this).children("a") ;
						var url = gotouri.attr("hreflang") ;
						
						$("#start_menu_panel").hide() ;$("#close").hide() ;
						gotoURL(url.indexOf("http") == -1?path+"/"+url:url) ;
						parseFuncBar(funcbarName,item.attr("id")) ;
						
					});
				}
			});
			second_panel_menu.html(_ul2) ;
		});
		first_panel.html(_ul1) ;
		
	}) ;
}

/** 功能菜单 */
function parseFuncBar(funcbar,barName) {
	var bar_left = $("#second_tabs_7") ;
	bar_left.empty() ;
	
	if(undefined != funcbar && undefined != barName) {
		$.post(path+"/xml_tree/"+funcbar+".xml",null,function(data,textStatus){
			
			var dataXml = $(data) ;
			var menus = dataXml.find(barName).children("menus") ;
			
			menus.each(function(){
				var funcMenu = $(this) ;
				var url = funcMenu.text() ;
				var funcMenuURL = "<a hidefocus='hidefocus' href='javascript:gotoURL(\""+path+"/"+url+"\");' title='' class='active'><span>"+funcMenu.attr("text")+"</span></a>" ;
				
				bar_left.append(funcMenuURL) ;
				
			});
		});
	} else {
		alert("funcbar和barName为空！") ;
	}
}

//打开页面
function gotoURL(url) {
	var iframe = $("#iframe") ;
	iframe.attr("src",url) ;
}

function indexMenuListView(url) {
	var listView = $("#start_menu_panel") ;
	var close = $("#close") ;
	
	//菜单关闭
	close.show() ;
	//菜单显示
	listView.show() ;
	
	var listViewOffset = listView.offset() ;
	close.css("position","absolute").css("z-index",288888).css("cursor","pointer") ;
	close.css("left",listView.width()-17).css("top",listViewOffset.top + 12) ;
	close.click(function() {
		listView.hide() ;
		close.hide() ;
	}) ;
}


function exit() {
	window.location.href=path+"/web_manager/login.jsp" ;
}








